﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CCWin.SkinControl;
using GroupChat2013.Entity;
using System.IO;
using CCWin;

namespace GroupChat2013.Main.Controls
{
    public partial class UploadPanel : SkinPanel
    {
        /// <summary>
        /// 最大同时上传个数
        /// </summary>
        private const int MAX_UPLOAD = 5;

        /// <summary>
        /// 同时拖拽文件上限
        /// </summary>
        private const int MAX_SELECTEDITEM = 3;

        /// <summary>
        /// 上传文件最大大小 单位b
        /// </summary>
        private const double MAXSIZE = 100 * 1024 * 1024;

        public int m_OrgX = 0;
        public int m_OrgY = 6;
        private const int itemWidth = 184;
        private const int itemHeight = 61;

        /// <summary>
        /// 子控件个数
        /// </summary>
        public int ControlCount { get { return this.Controls.Count; } }

        /// <summary>
        /// 构造函数
        /// </summary>
        public UploadPanel()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 添加一个上传文件控件
        /// </summary>
        public bool AddUploadItem(string filePath, ObjTalker objTalker, Action<ShareResultInfo> uploadCompeled)
        {
            string resultMSG = CheckCondition(filePath);
            if (!string.IsNullOrEmpty(resultMSG))
            {
                MessageBoxEx.Show(resultMSG, "提示",
                     MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            UploadItem item = new UploadItem(this, filePath, objTalker);
            item.UploadCompeled += uploadCompeled;
            this.AddControl(item);
            return true;
        }

        /// <summary>
        /// 取消全部上传
        /// </summary>
        public void CancelAllUpload()
        {
            foreach (Control control in this.Controls)
            {
                UploadItem item = control as UploadItem;
                if (item != null)
                {
                    item.CancelUpload();
                    item.Dispose();
                }
            }
        }

        /// <summary>
        /// 按照正确的样式显示控件
        /// </summary>
        /// <param name="item"></param>
        private void AddControl(UploadItem item)
        {
            int y = ControlCount == 0 ? m_OrgY : m_OrgY + ControlCount * (itemHeight);
            item.Location = new Point(m_OrgX, y);
            this.Controls.Add(item);
        }

        /// <summary>
        /// 检查上传条件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string CheckCondition(string path)
        {
            string result = string.Empty;
            bool exist = false;
            foreach (Control c in this.Controls)
            {
                UploadItem p = c as UploadItem;
                if (p != null && p.FilePath.Equals(path))
                {
                    exist = true;
                    break;
                }
            }
            if (exist)
            {
                result = "该文件已添加到队列！";
            }
            else
            {
                if (this.Controls.Count >= MAX_UPLOAD)
                {
                    result = "上传队列个数已经达到最大值";
                    return result;
                }
                FileInfo fi = new FileInfo(path);
                if (fi.Length > MAXSIZE)
                {
                    result = "发送文件大小超过最大限制";
                }
            }

            return result;
        }
    }
}
